package cn.vw.hi.list.persistence;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.vw.hi.commen.tools.Page;
import cn.vw.hi.list.model.Business;

public class BusinessDaoImpl extends HibernateDaoSupport implements BusinessDao {

	//添加商家
	public boolean addBusiness(Business business) {
		try
		{
			this.getHibernateTemplate().save(business);
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}

	//获得商家列表
	public List businessList(final Page pageObj,final  String searchStr) {
		List list=(List)this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				String hql="from Business b,Province p,City c,Counties ct,Loads l where b.loadId=l.id and l.provinceId=p.id  and l.cityId=c.id and l.countiesId=ct.id ";
				if(searchStr!=null&&!searchStr.equals(""))
				{
					String[] str=searchStr.split("_");
					if(!str[0].equals("-1"))
					{
						hql+="  and l.provinceId='"+str[0]+"'";
						if(!str[1].equals("-1"))
						{
							hql+="  and l.cityId='"+str[1]+"'";
							if(!str[2].equals("-1"))
							{
								hql+="  and l.countiesId='"+str[2]+"'";
								if(!str[3].equals("-1"))
								{
									hql+="  and l.id='"+str[3]+"'";
								}
							}
						}
					}
						
				}
				Query query =session.createQuery(hql);
				query.setFirstResult((pageObj.getCurrentPage()-1)*pageObj.getPageSize());
				query.setMaxResults(pageObj.getPageSize());
				int rowCount = getRowCount(hql);
		         pageObj.setRowCount(rowCount);
		         pageObj.setTotalPage(rowCount);
		         pageObj.setPreNextPage();
				return query.list();
			}
		});
		return list;
	}
	/**
	 * 获得记录数
	 */
	public int getRowCount(final String hql) {
		Integer rowCount = 0;
		rowCount = (Integer) this.getHibernateTemplate().execute(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						return session.createQuery(hql).list().size();
					}
				});
		return rowCount;
	}

	//删除商家
	public boolean deleteBusiness(final String ids) {
		this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				String hql="delete from Business b where b.id in("+ids+")";
				Query query =session.createQuery(hql);
				query.executeUpdate();
				return true;
			}
		});
		return true;
	}

	//查找商家
	public List findBusiness(final Integer id) {
		List list=(List)this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session)throws HibernateException, SQLException {
				String hql="from Business b,Province p,City c,Counties ct,Loads l where b.loadId=l.id and l.provinceId=p.id  and l.cityId=c.id and l.countiesId=ct.id and b.id="+id;
				Query query =session.createQuery(hql);
				return query.list();
			}
		});
		return list;
	}

	//修改商家
	public boolean modifyBusiness(Business business) {
		try
		{
		Business b=(Business)this.getHibernateTemplate().get(Business.class, business.getId());
		BeanUtils.copyProperties(b, business);
		this.getHibernateTemplate().update(b);
		}catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		return true;
	}
}
